package com.bw.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.pojo.TbBrandExample;
import com.pinyougou.pojo.TbBrandVo;
import com.pinyougou.sellergoods.service.IBrandService;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@Service
public class BrandService implements IBrandService {


    @Autowired
    private TbBrandMapper brandMapper;

    @Override
    public Result addTbBrand(TbBrand brand) {
        brandMapper.insert(brand);
        return new Result(true, "添加成功");
    }

    @Override
    public Result updTbBrand(TbBrand brand) {
        try {
            brandMapper.updateByPrimaryKey(brand);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "修改失败");
        }
    }

    @Override
    public Result delById(Long[] idList) {
        for (Long id : idList) {
            brandMapper.deleteByPrimaryKey(id);
        }
        return new Result(true, "删除成功");
    }

    @Override
    public List<Map> findList() {
        return brandMapper.findList();
    }

    @Override
    public Result getById(Long id) {
        TbBrand tbBrand = brandMapper.selectByPrimaryKey(id);
        return new Result(true, "获取成功");
    }

    @Override
    public PageResult findByPagination(int pageNum, int pageSize, String name) {

        PageHelper.startPage(pageNum,pageSize);

        TbBrandExample tbBrandExample = null;

        //判断是否使用条件
        if (name != null && name.length()>0) {

            System.out.println("条件查询！！！！！！！！！！name:"+name);

            //实例化example类
            tbBrandExample = new TbBrandExample();

            //获取条件类
            TbBrandExample.Criteria criteria = tbBrandExample.createCriteria();

            criteria.andNameLike("%"+name+"%");
        }


        Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(tbBrandExample);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<TbBrand> findAll() {
        return brandMapper.selectByExample(null);
    }
}
